<style>
    /* 限制 来源 列的宽度 */
    tr>*:nth-child(3),
    td>*:nth-child(3) {
        max-width: 100px;
    }
</style>
<p>
    <a href="<%= pageUrl %>../">返回上一级</a>
    &nbsp;
    <a href="<%= pageUrl %>../upload" target="_blank">上传文件</a><br><br>
    您现在在「文件管理 > 文件对象管理」，以下是系统所有文件对象信息
</p>
<script>
    list({ tableElementId: "book-table" });

    function list({ tableElementId }) {
        postRequest("/file/object/list", { token: localStorageUtils.getToken() })
            .then(function (responseData) {
                var axiosData = responseData.data;
                var status = axiosData.status;
                var data = axiosData.data;
                if (status === "success") {
                    // console.log(data);

                    // 数据进行转换
                    var renderData = [];
                    data.forEach(element => {
                        console.log(element);
                        renderData.push({
                            编号: `${element.id}`,
                            关联文件: `<span class="overflow-omit" style="margin: 0 auto;">
                                        ID: ${element.fileId}
                                        <a href="<%= pageUrl %>../detail?id=${element.fileId}">查看</a>
                                    </span>`,
                            链接: `<span class="overflow-omit" style="font-size: 12px; line-height: 1.2em; display: block;">
                                        ${element.filePath}
                                    </span>`,
                            密码: `<span class="overflow-omit" style="font-size: 12px; line-height: 1.2em; display: block;"><nobr>
                                        文件密码: ${element.filePwd}<br>
                                        提取码: ${element.fileShareCode}
                                    </nobr></span>`,
                            存储介质: `<span class="overflow-omit" style="font-size: 12px; line-height: 1.2em; display: block;"><nobr>
                                        ${element.storageMediumForDisplay}
                                    </nobr></span>`,
                            状态: `${(element.uploadStatus ? element.uploadStatus : "<span style='color: grey; font-weight: bold;'>未知</span>")
                                .replace("SUCCESS", "<span style='color: green; font-weight: bold;'>成功</span>")
                                .replace("UPLOADING", "<span style='color: orange; font-weight: bold;'>正在上传</span>")
                                .replace("NOT_EXIST", "<span style='color: red; font-weight: bold;'>不存在</span>")}`,
                            管理: `<span span class="overflow-omit" style="margin: 0 auto;">
                                        <a href="javascript:refreshFileObjectStatus(${element.id});" style="${element.storageMedium == "QCLOUD_COS" ? "" : "display: none;"}">刷新状态</a>
                                        <a href="<%= pageUrl %>../get-share-url?id=${element.id}&fileId=${element.fileId}">修改</a>
                                        <a href="javascript:deleteFileObject(${element.id});">删除</a>
                                    </span >`,
                        })
                    });
                    if (renderData.length == 0) {
                        function htmlEncode(str) {
                            // refer: https://stackoverflow.com/questions/4183801/escape-html-chracters
                            var div = document.createElement('div');
                            var txt = document.createTextNode(str);
                            div.appendChild(txt);
                            return div.innerHTML;
                        }
                        renderTable({ data: `暂无文件`, tableId: tableElementId, renderTableHead: true });
                    } else {
                        renderTable({ data: renderData, tableId: tableElementId, renderTableHead: true });
                    }
                } else {
                    swal(`出错啦！${data.errMsg} (错误码: ${data.errCode})`);
                }
            }).catch(function (error) {
                console.log(error);
                swal("无法连接到服务器，请检查网络连接！");
            });
    }

    function refreshFileObjectStatus(fileObjectId) {
        postRequest("/file/object/refreshFileObjectStatus", { token: localStorageUtils.getToken(), fileObjectId: fileObjectId })
            .then(function (responseData) {
                var axiosData = responseData.data;
                var status = axiosData.status;
                var data = axiosData.data;
                if (status === "success") {
                    console.log(data);
                    swal("刷新成功！");
                    list({ tableElementId: "book-table" });
                } else {
                    swal(`出错啦！${data.errMsg} (错误码: ${data.errCode})`);
                }
            }).catch(function (error) {
                console.log(error);
                swal("无法连接到服务器，请检查网络连接！");
            });
    }

    // 删除文件对象
    function deleteFileObject(deleteFileObjectId) {
        if (!confirm(`确认要删除编号为 ${deleteFileObjectId} 的文件对象吗？`)) return;

        postRequest("/file/object/delete", { token: localStorageUtils.getToken(), id: deleteFileObjectId })
            .then(function (responseData) {
                var axiosData = responseData.data;
                var status = axiosData.status;
                var data = axiosData.data;
                if (status === "success") {
                    console.log(data)
                    if (data == "success") {
                        list({ tableElementId: "book-table" });
                        swal("删除成功！");
                    } else {
                        swal("删除失败！");
                    }
                } else {
                    swal(`出错啦！${data.errMsg} (错误码: ${data.errCode})`);
                }
            }).catch(function (error) {
                console.log(error);
                swal("无法连接到服务器，请检查网络连接！");
            });
    }
</script>